package com.fxp.project.jiuselu.web.models.shareprofit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fxp.project.jiuselu.web.base.controller.BaseController;
import com.fxp.project.jiuselu.web.business.v1.pojo.CustomManagerVo;
import com.fxp.project.jiuselu.web.business.v1.request.user.CustomManagerByUserId;
import com.fxp.project.jiuselu.web.models.shareprofit.entity.JslShareProfitRecord;
import com.fxp.project.jiuselu.web.models.shareprofit.mapper.JslShareProfitRecordMapper;
import com.fxp.project.jiuselu.web.models.shareprofit.request.GetRecord;
import com.fxp.project.jiuselu.web.models.shareprofit.service.IJslShareProfitRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fxp.project.jiuselu.web.models.shareprofit.vo.JslShareProfitRecordVo;
import com.fxp.project.jiuselu.web.models.user.entity.JslUser;
import com.fxp.project.jiuselu.web.models.user.entity.JslUserDelay;
import com.fxp.project.jiuselu.web.models.user.mapper.JslUserDelayMapper;
import com.fxp.project.jiuselu.web.models.user.service.IJslUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 分润记录表 服务实现类
 * </p>
 *
 * @author M_
 */
@Service
public class JslShareProfitRecordServiceImpl extends ServiceImpl<JslShareProfitRecordMapper, JslShareProfitRecord> implements IJslShareProfitRecordService {

    @Autowired
    private JslShareProfitRecordMapper jslShareProfitRecordMapper ;
    @Autowired
    private IJslUserService jslUserService;
    @Autowired
    private JslUserDelayMapper userDelayMapper;

    @Override
    public PageInfo<JslShareProfitRecord> selectRecordByUserId(GetRecord getRecordVo) {
//        PageHelper.startPage(getRecordVo.getPage(),getRecordVo.getSize());
//        List<JslShareProfitRecord> jslShareProfitRecords = jslShareProfitRecordMapper.selectRecordByUserId(getRecordVo.getUserId());

        Long userId = BaseController.getUser().getUserId();
        List<JslShareProfitRecord> jslShareProfitRecords=jslShareProfitRecordMapper.
                selectList(new QueryWrapper<JslShareProfitRecord>().lambda()
                        .eq(JslShareProfitRecord::getCreateUser,getRecordVo.getUserId())
                        .eq(JslShareProfitRecord::getUserId,userId)
                );
        if (jslShareProfitRecords.size()!=0){
            List<Long> collect = jslShareProfitRecords.stream().map(JslShareProfitRecord::getCreateUser).collect(Collectors.toList());
            List<JslUser> list = jslUserService.list(new QueryWrapper<JslUser>().lambda().in(JslUser::getUserId, collect));
            Map<Long,String> map=new HashMap<>();
            list.forEach(e->{
                map.put(e.getUserId(),e.getNickName());
            });

            jslShareProfitRecords.forEach(e->{
                String s = map.get(e.getCreateUser());
                if (s!=null){
                    e.setName(map.get(e.getCreateUser()));
                }
            });
        }

        return new PageInfo<>(jslShareProfitRecords);
    }

    @Override
    public PageInfo<CustomManagerVo> customManager(GetRecord getRecordVo) {
        Long userId = getRecordVo.getUserId();

        Map<Long, CustomManagerVo> map = new HashMap<>();

        Long loginUserId = Objects.requireNonNull(BaseController.getUser()).getUserId();

        //他的客户详情
        List<JslShareProfitRecord> jslShareProfitRecords = jslShareProfitRecordMapper.selectList(new QueryWrapper<JslShareProfitRecord>().lambda()
                .eq(JslShareProfitRecord::getLevel, 0)
                .eq(JslShareProfitRecord::getThrCreate, userId)
                .eq(JslShareProfitRecord::getUserId,loginUserId));

        map=jslUserService.getJslShareProfitRecordMap(jslShareProfitRecords,map,6);
        map=jslUserService.getJslShareProfitRecordMapNoThing(userId,map,1);
        map=jslUserService.getJslShareProfitRecordNum(map);

        PageInfo<CustomManagerVo> pageInfo = new PageInfo<>(new ArrayList<>(map.values()));
        pageInfo.setTotal(map.size());
        return pageInfo;
    }


    @Override
    public PageInfo<CustomManagerVo> twoClient(GetRecord getRecordVo) {
        Long userId = getRecordVo.getUserId();

        Map<Long, CustomManagerVo> map = new HashMap<>();

        Long loginUserId = Objects.requireNonNull(BaseController.getUser()).getUserId();

        //他的客户详情
        List<JslShareProfitRecord> jslShareProfitRecords = jslShareProfitRecordMapper.selectList(new QueryWrapper<JslShareProfitRecord>().lambda()
                .eq(JslShareProfitRecord::getLevel, 0)
                .eq(JslShareProfitRecord::getTwoCreate, userId)
                .eq(JslShareProfitRecord::getUserId,loginUserId));

        map=jslUserService.getJslShareProfitRecordMap(jslShareProfitRecords,map,3);

        map=jslUserService.getJslShareProfitRecordMapNoThing(userId,map,5);



        PageInfo<CustomManagerVo> pageInfo = new PageInfo<>(new ArrayList<>(map.values()));
        pageInfo.setTotal(map.size());
        return pageInfo;
    }

    @Override
    public PageInfo<CustomManagerVo> client() {
        Long userId = Objects.requireNonNull(BaseController.getUser()).getUserId();

        Map<Long, CustomManagerVo> map = new HashMap<>();

        //他的客户详情
        List<JslShareProfitRecord> jslShareProfitRecords = jslShareProfitRecordMapper.selectList(new QueryWrapper<JslShareProfitRecord>().lambda()
                .eq(JslShareProfitRecord::getLevel, 2)
                .eq(JslShareProfitRecord::getUserId,userId));

//        List<JslShareProfitRecord> jslShareProfitRecords = jslShareProfitRecordMapper.selectDelayByUserIdAndLevel(userId, 2);

//        jslShareProfitRecords.stream().filter(e->{
//            JslUserDelay user = userDelayMapper.selectOne(new QueryWrapper<JslUserDelay>().eq("create_by", e.getCreateUser()));
//            System.out.println(user);
//            return true;
//        });

        for(JslShareProfitRecord shareProfitRecord : jslShareProfitRecords){
            JslUserDelay user = userDelayMapper.selectOne(new QueryWrapper<JslUserDelay>().eq("create_by", shareProfitRecord.getCreateUser()));
            if(user==null)
                shareProfitRecord.setState(0L);
            else
                shareProfitRecord.setState(user.getState());
        }
        map=jslUserService.getJslShareProfitRecordMap(jslShareProfitRecords,map,7);
        map=jslUserService.getJslShareProfitRecordMapNoThing(userId,map,5);

        PageInfo<CustomManagerVo> pageInfo = new PageInfo<>(new ArrayList<>(map.values()));
        pageInfo.setTotal(map.size());
        return pageInfo;
    }
}
